﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TreeGraphicalControl;
using FeatureModelCore;
using Collections.TreeCollections;
using System.Windows.Controls;
using System.Windows;
using FeatureModelTool.FeatureModelView.MenuItems;
using FeatureModelTool.Windows;
using FeatureModelCore.FMTypes;
using System.Windows.Media;
using FeatureModelCore.FMCrossTreeConstraint;

namespace FeatureModelTool.FeatureModelView.TreeViewItems
{
    public class TreeItem_Constraint : TreeViewItemControl
    {
        public CrossTreeConstraint FeatureInstance { get; protected set; }
        protected Brush Backgroud
        {
            get
            {
                if (FeatureInstance.ExistError)
                    return FeatureTreeView.ERROR_BACKGROUND;
                if (FeatureInstance.ExistWarning)
                    return FeatureTreeView.WARNING_BACKGROUND;
                return FeatureTreeView.COMMON_BACKGROUND;
            }
        }
        //private readonly static string _ICON_SOURCE = "/FeatureModelTool;component/Images/c.png";
        private readonly static Brush _COMMON_COLOR = Brushes.White;

        public TreeItem_Constraint(CrossTreeConstraint constraint)
        {
            FeatureInstance = constraint;
            this.Header = string.Format("Cross Tree Constraint: {0}", FeatureInstance.ToString());

            //this.Focusable = false;
        }

        /// <summary>
        /// Tree Item Construct
        /// </summary>
        /// <param name="cons"></param>
        /// <returns></returns>
        public static TreeItem_Constraint ConstructTreeItem(CrossTreeConstraint cons)
        {
            return new TreeItem_Constraint(cons);
        }

        #region Context Menu
        /// <summary>
        /// Context Generator
        /// </summary>
        /// <param name="treeRoot"></param>
        /// <returns></returns>
        public static ContextMenu GenerateContext(TreeItem_Constraint treeItemCons, CrossTreeConstraint cons)
        {
            // edit type
            MenuItem_Constraint editType = new MenuItem_Constraint(treeItemCons, cons)
            {
                Header = "Edit Type"
            };
            editType.Click += new RoutedEventHandler(Edit_EvenetHandler);

            // delete
            MenuItem_Constraint delete = new MenuItem_Constraint(treeItemCons, cons)
            {
                Header = "Delete"
            };
            delete.Click += new RoutedEventHandler(TreeItem_Root.Delete_EventHanlder);

            ContextMenu contextMenu = new ContextMenu();

            contextMenu.Items.Add(editType);
            contextMenu.Items.Add(delete);
            return contextMenu;
        }

        /// <summary>
        /// Edit Type of cross contraints
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void Edit_EvenetHandler(object sender, RoutedEventArgs e)
        {
            MenuItem_Constraint cons = sender as MenuItem_Constraint;
            if (cons == null)
            {
                return;
            }
            Array types = Enum.GetValues(typeof(FMCrossTreeConstraintType));
            EditTypeDialog dia = new EditTypeDialog(types, cons.FeatureInstance.Type);
            dia.ShowDialog();

            if (dia.Dirty == true)
            {
                cons.FeatureInstance.Type = (FMCrossTreeConstraintType)dia.RType;
            }
        }
        #endregion

    }
}
